<?php
error_reporting(0);
$isadmin=1; 
include '../comm/config.php';
include 'check.php';
require_once 'reader.php'; 
$uptypes=array('application/vnd.ms-excel'); 
$max_file_size=5000000;   //上传文件大小限制, 单位BYTE
$destination_folder="upload/"; //上传文件路径
//$watermark=1;   //是否附加水印(1为加水印,其他为不加水印);
//$watertype=1;   //水印类型(1为文字,2为图片)
//$waterposition=1;   //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);
//$waterstring="newphp.site.cz"; //水印字符串
//$waterimg="xplore.gif";  //水印图片
//$imgpreview=1;   //是否生成预览图(1为生成,其他为不生成);
//$imgpreviewsize=1/2;  //缩略图比例
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理中心</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.STYLE1 {color: #FF3300}
-->
</style>
</head>

<body topmargin="5" leftmargin="0">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="282" valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td>
          <table cellspacing="0" width="100%" height="207" style="border:1px  solid #DCEAF7;">
            <tr>
              <td height="30" align="left" bgcolor="E9F2FB" class="bigtext"><strong>&nbsp;<img src="images/arrow.gif" width="16" height="22" align="absmiddle" /> 导入交易记录</strong></td>
              </tr>
            <tr>
              <td valign="top"><form enctype="multipart/form-data" method="post" name="upform">
<br />
<br />
<table width="560" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="40" colspan="2" class="bigtext STYLE1">提示：将您登录阿里妈妈后的成交记录数据导出成.xls文件直接可以进行导入！导入的订单无法自动识别购物会员ID，需客户找回订单。</td>
    </tr>
  <tr>
    <td width="129" height="40" class="bigtext">选择导入的文件：</td>
    <td width="431" height="40" class="bigtext"><input name="upfile" type="file" size="17" />
允许上传的文件类型为:xls </td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
    <td height="40"><input type="submit" value="导入交易记录" size="17"></td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
    <td height="40"><?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if (!is_uploaded_file($_FILES["upfile"][tmp_name]))
//是否存在文件
{ 
echo "<font color='red'>文件不存在！</font>";
exit;
}

$file = $_FILES["upfile"];
if($max_file_size < $file["size"])
//检查文件大小
{
echo "<font color='red'>文件太大！</font>";
exit;
  }

if(!in_array($file["type"], $uptypes))
//检查文件类型
{
echo "<font color='red'>只能上传xls！</font>";
exit; 
}

if(!file_exists($destination_folder))
mkdir($destination_folder);

$filename=$file["tmp_name"];
$image_size = getimagesize($filename); 
$pinfo=pathinfo($file["name"]);
$ftype=$pinfo[extension];
//$destination = $destination_folder.time().".".$ftype;
$destination = $destination_folder."income.".$ftype;
//if (file_exists($destination) && $overwrite != true)
//{
//     echo "<font color='red'>同名文件已经存在了！</a>";
//     exit;
//}

if(!move_uploaded_file ($filename, $destination))
{
   echo "<font color='red'>移动文件出错！</a>";
     exit;
  }

$pinfo=pathinfo($destination);
$fname=$pinfo[basename];
//echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>".$destination_folder.$fname."</font><br>";
//echo " 宽度:".$image_size[0];
//echo " 长度:".$image_size[1];
//if($watermark==1)
//{
//$iinfo=getimagesize($destination,$iinfo);
//$nimage=imagecreatetruecolor($image_size[0],$image_size[1]);
//$white=imagecolorallocate($nimage,255,255,255);
//$black=imagecolorallocate($nimage,0,0,0);
//$red=imagecolorallocate($nimage,255,0,0);
//imagefill($nimage,0,0,$white);
//switch ($iinfo[2])
//{
//case 1:
//$simage =imagecreatefromgif($destination);
//break;
//case 2:
//$simage =imagecreatefromjpeg($destination);
//break;
//case 3:
//$simage =imagecreatefrompng($destination);
//break;
//case 6:
//$simage =imagecreatefromwbmp($destination);
//break;
//default:
//die("<font color='red'>不能上传此类型文件！</a>");
//exit;
//}

//imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]);
//imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white);
//
//switch($watertype)
//{
//case 1:  //加水印字符串
//imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black);
//break;
//case 2:  //加水印图片
//$simage1 =imagecreatefromgif("xplore.gif");
//imagecopy($nimage,$simage1,0,0,0,0,85,15);
//imagedestroy($simage1);
//break;
//} 
//
//switch ($iinfo[2])
//{
//case 1:
////imagegif($nimage, $destination);
//imagejpeg($nimage, $destination);
//break;
//case 2:
//imagejpeg($nimage, $destination);
//break;
//case 3:
//imagepng($nimage, $destination);
//break;
//case 6:
//imagewbmp($nimage, $destination);
////imagejpeg($nimage, $destination);
//break;
//}

//覆盖原上传文件
//imagedestroy($nimage);
//imagedestroy($simage);
//}
//
//if($imgpreview==1)
//{
//echo "<br>图片预览:<br>";
//echo "<a href=\"".$destination."\" target='_blank'><img src=\"".$destination."\" width=".($image_size[0]*$imgpreviewsize)." height=".($image_size[1]*$imgpreviewsize);
//echo " alt=\"图片预览:\r文件名:".$destination."\r上传时间:\" border='0'></a>";
//}
}
?>
<?php
if($destination!=""){
$link = mysql_connect($dbserver, $dbuser, $dbpass);
if (!$link) {
     die(ERR_DB);
}
// ExcelFile($filename, $encoding);  
$data = new Spreadsheet_Excel_Reader();  
// Set output Encoding.  
$data->setOutputEncoding('gbk');  
//”data.xls”是指要导入到mysql中的excel文件  
$data->read('upload/income.xls');  
//连接数据库 
 mysql_query("set names 'gbk'");
 //输出中文  
mysql_select_db($dbname);
//选择数据库  
error_reporting(E_ALL ^ E_NOTICE);  
$snum=0;
$fnum=0;
for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) 
{  
	//以下注释的for循环打印excel表数据   
//	for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) 
//	{  
//		echo "\"".$data->sheets[0]['cells'][$i][$j]."\",";  
//	}  
//	echo "\n";  
	$sql0="select Id from ".$BIAOTOU."tradelist where trade_id='".$data->sheets[0]['cells'][$i][12]."'";
	
	$rs=mysql_query($sql0);
	$num = mysql_num_rows($rs);
	if(!$num)
	{	$item_title=str_replace("'","",$data->sheets[0]['cells'][$i][2]);
		$shop_title=str_replace("'","",$data->sheets[0]['cells'][$i][4]);
		$sql = "INSERT INTO ".$BIAOTOU."tradelist(pay_time,item_title,num_iid,shop_title,pay_price,commission_rate,commission,item_num,trade_id) VALUES('".  $data->sheets[0]['cells'][$i][1]."','".$item_title."','".  $data->sheets[0]['cells'][$i][3]."','".$shop_title."','".  $data->sheets[0]['cells'][$i][5]."','".  $data->sheets[0]['cells'][$i][7]."','".  $data->sheets[0]['cells'][$i][8]."','".  $data->sheets[0]['cells'][$i][6]."','".  $data->sheets[0]['cells'][$i][12]."')";   
		$res = mysql_query($sql); 
		$snum=$snum+1;
	}
	else
	{
		$fnum=$fnum+1;
	}
}
echo "成功导入 ".$snum." 条交易记录！<br>";
echo "本次导入数据中，数据库中 ".$fnum." 条交易记录已经存在，导入过程忽略！<br>";
unlink("upload/income.xls");
mysql_close($link); }
?></td>
  </tr>
</table>

</form>

</td>
                </tr>
              </table></td>
            </tr>
          </table></td>
        </tr>
      
    </table></td>
  </tr>
</table>
</body>
</html>